# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc

class aas_security(osv.osv):
    _name = 'aas.security'
    _description = 'Security Implementation'
    _columns = {
    }
    
    def get_ids_from_objs(self, cr, uid, objs, context=None):
        ids = []
        for obj in objs:
            ids.append(obj.id)
        ids = list(set(ids))
        return ids
    
aas_security()


class aas_poweremail_templates(osv.osv):
    _inherit = 'poweremail.templates'
    _columns = {}
    
    def generate_mail(self,
                      cursor,
                      user,
                      template_id,
                      record_ids,
                      context=None):
        if context is None:
            context = {}
        res = []
        template = self.browse(cursor, user, template_id, context=context)
        if not template:
            raise Exception("The requested template could not be loaded")
        for record_id in record_ids:
            mailbox_id = self._generate_mailbox_item_from_template(
                                                                cursor,
                                                                user,
                                                                template,
                                                                record_id,
                                                                context)
            mail = self.pool.get('poweremail.mailbox').browse(
                                                        cursor,
                                                        user,
                                                        mailbox_id,
                                                        context=context
                                                              )
            if template.report_template:
                self._generate_attach_reports(
                                              cursor,
                                              user,
                                              template,
                                              record_id,
                                              mail,
                                              context
                                              )
            # Create a partner event
            cursor.execute("SELECT state from ir_module_module where state='installed' and name = 'mail_gateway'")
            mail_gateway = cursor.fetchall()
            if template.partner_event and mail_gateway:
                self._generate_partner_events(cursor,
                                              user,
                                              template,
                                              record_id,
                                              mail,
                                              context)
            self.pool.get('poweremail.mailbox').write(
                                                cursor,
                                                user,
                                                mailbox_id,
                                                {'folder':'outbox'},
                                                context=context
                                                      )
            res.append(mailbox_id)
        return res
    
aas_poweremail_templates()
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
